from coord_convert.transform import wgs2gcj, wgs2bd, gcj2wgs, gcj2bd, bd2wgs, bd2gcj
import json

with open('hubei.geojson','r') as fr:
    all = json.loads(fr.read())

for i in range(0, len(all['features'])):
    each = all['features'][i]
    if each['geometry']['type'] == 'Polygon':
        for k in range(0, len(each['geometry']['coordinates'][0])):
            coords = each['geometry']['coordinates'][0][k]
            lng = float(coords[0])
            lat = float(coords[1])
            newLon, newLat = gcj2wgs(lng, lat)
            all['features'][i]['geometry']['coordinates'][0][k] = [round(newLon, 6), round(newLat, 6)]

    elif each['geometry']['type'] == 'MultiPolygon':
        for k in range(0, len(each['geometry']['coordinates'])):
            plg = each['geometry']['coordinates'][k][0]
            for m in range(0, len(plg)):
                coords = plg[m]
                lng = float(coords[0])
                lat = float(coords[1])
                newLon, newLat = gcj2wgs(lng, lat)
                coords = [round(newLon, 6), round(newLat, 6)]
                all['features'][i]['geometry']['coordinates'][k][0][m] = [round(newLon, 6), round(newLat, 6)]

with open('hubei2.geojson','w') as fw:
    fw.write(json.dumps(all, separators=(',',':'), ensure_ascii=False))